package cn.zebra.dev.product.module.product.service.impl;

import cn.zebra.dev.common.utils.DateUtils;
import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.product.module.product.dao.ProductDao;
import cn.zebra.dev.product.module.product.entity.ProductEntity;
import cn.zebra.dev.product.module.product.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 商品表
 *
 * @author runnable
 * @date 2019-09-05 14:35:42
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {


    @Override
    public ProductEntity findById(Long id) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id, product_sn, product_name, product_image, total_num, unit_name,unit_price,sell_num");
        wrapper.apply("id = {0}", id);
        wrapper.apply("delete_flag = FALSE");
        return getOne(wrapper);
    }

    @Override
    public ProductEntity findById(Long id, Map<String, Object> params) {
        return baseMapper.findById(id, params);
    }

    @Override
    public Boolean saveProduct(ProductEntity entity, Long createId) {
        entity.setCreateId(createId);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateId(0L);
        entity.setUpdateTime(DateUtils.defaultTime());
        entity.setDeleteFlag(false);
        return save(entity);
    }

    @Override
    public Boolean updateProduct(Long id, ProductEntity entity, Long updateId) {
        entity.setCreateId(null);
        entity.setCreateTime(null);
        entity.setUpdateId(updateId);
        entity.setUpdateTime(LocalDateTime.now());
        return update(entity, "id = {0} AND delete_flag = FALSE", id);
    }

    @Override
    public PageUtils<ProductEntity> findPage(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.queryPage(page, params));
        return new PageUtils<>(page);
    }

    @Override
    public List<ProductEntity> findByIds(List<Long> productIds) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id", productIds);
        wrapper.apply("delete_flag = FALSE");
        return list(wrapper);
    }

    @Override
    public List<ProductEntity> findByCategoryIds(List<Long> categoryIds) {
        return baseMapper.findByCategoryIds(categoryIds);
    }

    @Override
    public PageUtils<ProductEntity> findPageByShopId(Map<String, Object> params) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("shop_id={0} AND delete_flag = FALSE", params.get("shopId"));
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>(params).getPage(),
                wrapper
        );
        return new PageUtils<>(page);
    }

    @Override
    public ProductEntity findProductIdByShopId(Long id) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id ");
        wrapper.apply("shop_id = {0}", id);
        return getOne(wrapper);
    }


    @Override
    public PageUtils<ProductEntity> findPageByProductName(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.findPageByProductName(page, params));
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<ProductEntity> findPageByCategoryIdAndProductName(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.findPageByCategoryIdAndProductName(page, params));
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<ProductEntity> findPageByBrandId(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.findPageByBrandId(page, params));
        return new PageUtils<>(page);
    }

    @Override
    public PageUtils<ProductEntity> findPageByGroupIdAndProductName(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.findPageByGroupIdAndProductName(page, params));
        return new PageUtils<>(page);
    }


    @Override
    public PageUtils<ProductEntity> findByCategoryId(Map<String, Object> params) {
        Page<ProductEntity> page = new Query<ProductEntity>(params).getPage();
        page.setRecords(baseMapper.findByCategoryId(page, params));
        return new PageUtils<>(page);
    }
}
